﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dazaza.Framework
{
    public static class Math
    {
        /// <summary>
        /// 将指定的小数按照舍去法进行舍入
        /// </summary>
        /// <param name="d">要舍入的小数。</param>
        /// <param name="decimals">返回值中的小数位数。</param>
        /// <returns></returns>
        public static string Round(decimal d, int decimals)
        {
            string s = d.ToString();
            if (s.Contains("."))
            {
                if (s.IndexOf('.') + decimals + 1 <= s.Length)
                {
                    return s.Substring(0, s.IndexOf('.') + decimals + 1);
                }
                return s;
            }
            return s;
        }
        /// <summary>
        /// 将指定的小数按照舍去法进行舍入
        /// </summary>
        /// <param name="d">要舍入的小数。</param>
        /// <param name="decimals">返回值中的小数位数。</param>
        /// <returns></returns>
        public static string Round(double d, int decimals)
        {
            string s = d.ToString();
            if (s.Contains("."))
            {
                if (s.IndexOf('.') + decimals + 1 <= s.Length)
                {
                    return s.Substring(0, s.IndexOf('.') + decimals + 1);
                }
                return s;
            }
            return s;
        }
        /// <summary>
        /// 将指定的小数按照舍去法进行舍入
        /// </summary>
        /// <param name="d">要舍入的小数。</param>
        /// <param name="decimals">返回值中的小数位数。</param>
        /// <returns></returns>
        public static decimal Round(decimal d, int decimals, decimal nothing)
        {
            return decimal.Parse(Round(d, decimals));
        }
        /// <summary>
        /// 将指定的小数按照舍去法进行舍入
        /// </summary>
        /// <param name="d">要舍入的小数。</param>
        /// <param name="decimals">返回值中的小数位数。</param>
        /// <returns></returns>
        public static double Round(double d, int decimals, double nothing)
        {
            return double.Parse(Round(d, decimals));
        }
    }
}
